<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>基础知识</title>
	<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css"" rel="stylesheet">
	<link href="https://cdn.bootcdn.net/ajax/libs/animate.css/3.2.3/animate.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="../../static/css/public.css" />
</head>
<body>
	<!-- 回到顶部 -->
	<div class="topBtn">
		<a href="#top">
			<i class="fa fa-long-arrow-up"></i>
		</a>
	</div>
	<!--右边内容-->
	<div id="app" class="main animated fadeInRight">
		<div class="shadow" v-cloak>
			<div class="Table" v-cloak id="top">
				<div class="TableRow" v-for="(el,index) in list">
					<a class="RowLeft" 
					:title="el.name"
					:href="'#'+el.id">{{el.name}}</a>
					<span class="RowRight">{{el.explain}}</span>
				</div>
			</div>	
			<div>
				  <div class="container">
				    <input type="file" id="input-img" @change="compress" />
				    <a :download="fileName" :href="compressImg" >普通下载</a>
				    <button @click="downloadImg">兼容 IE 下载</button>
				    <div>
				      <img :src="compressImg" />
				    </div>
				  </div>
			</div>
		</div>
	</div>
	<!--页面加载进度条-->
	<script src="https://cdn.bootcdn.net/ajax/libs/pace/0.5.0/pace.min.js"></script>
	<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.17/vue.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var app = new Vue({
			el: "#app",
			data: function () {
				return {
				    compressImg: null,
				    fileName: null,
					list:[
						{name:"push()",id:"push",explain:'尾部添加'},
						{name:"unshift()",id:"unshift",explain:'头部添加'},
						{name:"pop()",id:"pop",explain:'尾部删除'},
					]
				}
			},
		  methods: {
			compress() {
			  // 获取文件对象
			  const fileObj = document.querySelector('#input-img').files[0];
			  // 获取文件名称，后续下载重命名
			  this.fileName = `${new Date().getTime()}-${fileObj.name}`;
			  // 获取文件后缀名
			  const fileNames = fileObj.name.split('.');
			  const type = fileNames[fileNames.length-1];
			  // 压缩图片
			  this.handleCompressImage(fileObj, type);
			},
			handleCompressImage(img, type) {
			  const vm = this;
			  let reader = new FileReader();
			  // 读取文件
			  reader.readAsDataURL(img);
			  reader.onload = function(e) {
				let image = new Image(); //新建一个img标签
				image.src = e.target.result;
				image.onload = function() {
				  let canvas = document.createElement('canvas');
				  let context = canvas.getContext('2d');
				  // 定义 canvas 大小，也就是压缩后下载的图片大小
				  let imageWidth = image.width; //压缩后图片的大小
				  let imageHeight = image.height;
				  canvas.width = imageWidth;
				  canvas.height = imageHeight;
				  
				  // 图片不压缩，全部加载展示
				  context.drawImage(image, 0, 0);
				  // 图片按压缩尺寸载入
				  // let imageWidth = 500; //压缩后图片的大小
				  // let imageHeight = 200;
				  // context.drawImage(image, 0, 0, 500, 200);
				  // 图片去截取指定位置载入
				  // context.drawImage(image,100, 100, 100, 100, 0, 0, imageWidth, imageHeight);
				  vm.compressImg = canvas.toDataURL(`image/${type}`);
				};
			  };
			},
			// base64 图片转 blob 后下载
			downloadImg() {
			  let parts = this.compressImg.split(';base64,');
			  let contentType = parts[0].split(':')[1];
			  let raw = window.atob(parts[1]);
			  let rawLength = raw.length;
			  let uInt8Array = new Uint8Array(rawLength);
			  for(let i = 0; i < rawLength; ++i) {
				uInt8Array[i] = raw.charCodeAt(i);
			  }
			  const blob = new Blob([uInt8Array], {type: contentType});
			  this.compressImg = URL.createObjectURL(blob);
			  if (window.navigator.msSaveOrOpenBlob) {
				// 兼容 ie 的下载方式
				window.navigator.msSaveOrOpenBlob(blob, this.fileName);
			  }else{
				const a = document.createElement('a');
				a.href = this.compressImg;
				a.setAttribute('download', this.fileName);
				a.click();
			  }
			}
		  }
		})
	</script>
</body>

</html>